﻿using Fluffy.Library.DocumentContainer;
using Fluffy.Library.Mvvm;
using Fluffy.Library.Mvvm.Commands;
using Fluffy.Library.Utils;
using MonoTorrent.Common;
using System.Collections;
using System.Linq;
using System.Windows.Input;
using System.Windows.Shell;

namespace Fluffy.Torrent
{
	public class TorrentDocumentViewModel : DocumentViewModelBase
	{
		private readonly TorrentDocumentModel _model;

		internal TorrentDocumentViewModel(TorrentDocumentModel model)
		{
			_model = model;
		}

		public TorrentFileViewModel[] Files
		{
			get { return _model.Files.Select(x => new TorrentFileViewModel(x)).ToArray(); }
		}

		public string Name
		{
			get { return _model.Name; }
		}

		public string Comment
		{
			get { return _model.Comment; }
		}

		public string CreatedBy
		{
			get { return _model.CreatedBy; }
		}

		public string PublisherUrl
		{
			get { return _model.PublisherUrl; }
		}

		public string Publisher
		{
			get { return _model.Publisher; }
		}

		public string TorrentPath
		{
			get { return _model.TorrentPath; }
		}

		public string CreatedDate
		{
			get { return _model.CreatedDate; }
		}

		public string Size
		{
			get { return TitleUtils.GetTitleForSize(_model.Size); }
		}

		public override ICommand CutCommand
		{
			get { return new DisabledCommand(); }
		}

		public override ICommand CopyCommand
		{
			get { return new DisabledCommand(); }
		}

		public override ICommand PasteCommand
		{
			get { return new DisabledCommand(); }
		}

		public override ICommand SaveCommand
		{
			get { return new DisabledCommand(); }
		}

		public override ICommand SaveAsCommand
		{
			get { return new DisabledCommand(); }
		}

		public override CommandsContainer Menu1
		{
			get
			{
				CommandsContainer container = new CommandsContainer("Torrent");
				container.Children.Add(new CommandItem("Start", StartCommand));
				return container;
			}
		}

		public override CommandsContainer Menu2
		{
			get { return new CommandsContainer(); }
		}

		public override CommandsContainer Menu3
		{
			get { return new CommandsContainer(); }
		}

		public override void Close()
		{
			_model.Dispose();
		}

		public ICommand StartCommand
		{
			get { return new Command(OnStart); }
		}
		
		public ICommand SetNormalPriorityCommand
		{
			get { return new Command<IList>(list => SetPriority(list, Priority.Normal)); }
		}

		public ICommand SetDoNotDownloadPriorityCommand
		{
			get { return new Command<IList>(list => SetPriority(list, Priority.DoNotDownload)); }
		}

		public ICommand SetHighPriorityCommand
		{
			get { return new Command<IList>(list => SetPriority(list, Priority.High)); }
		}

		public ICommand SetHighestPriorityCommand
		{
			get { return new Command<IList>(list => SetPriority(list, Priority.Highest)); }
		}

		public ICommand SetImmediatePriorityCommand
		{
			get { return new Command<IList>(list => SetPriority(list, Priority.Immediate)); }
		}

		public ICommand SetLowPriorityCommand
		{
			get { return new Command<IList>(list => SetPriority(list, Priority.Low)); }
		}

		public ICommand SetLowestPriorityCommand
		{
			get { return new Command<IList>(list => SetPriority(list, Priority.Lowest)); }
		}

		private static void SetPriority(IList list, Priority priority)
		{
			if (list == null)
				return;

			for (int index = 0; index < list.Count; index++)
			{
				TorrentFileViewModel file = (TorrentFileViewModel) list[index];
				file.Priority = priority;
			}
		}

		private void OnStart()
		{
			string path = PathUtils.SelectDirectory("Select outpt directory.");

			if (string.IsNullOrEmpty(path))
				return;

			ThreadUtils.StartThread(() =>
			{
				try
				{
					SetProgress(0, 0);
					_model.ProgressChanged += SetProgress;

					_model.Load(path);
				}
				finally
				{
					_model.ProgressChanged -= SetProgress;
					ProgressState = TaskbarItemProgressState.None;
				}
			});
		}

		private void SetProgress(double progress, int speed)
		{
			ProgressValue = progress / 100D;
			ProgressMessage = string.Format("Loading... {0}/S", TitleUtils.GetTitleForSize(speed));
			ProgressState = TaskbarItemProgressState.Normal;
		}
	}
}
